package com.trans.handler;

import com.trans.anno.Translator;
import com.trans.auxiliary.HandleFullRecord;
import com.trans.auxiliary.HandleRecord;
import com.trans.auxiliary.TranslatorAbleHelper;
import com.trans.contants.TranslatorConfig;
import com.trans.entity.TransEntity;
import com.trans.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *@Description TODO
 *@Author xwduo
 *@Date 2022年03月01日 08:51
 **/
public interface TranslateHandle<K,V> {

    Logger log = LoggerFactory.getLogger(TranslateHandle.class);

    /**
     * @Description:  翻译处理方法
     * @Author: xwduo
     * @Date: 2022-10-28 17:09
     * @param：
     *		jsonAnno: 注解对象
     *	   origin：当前层级对象
     *		item: 做记录的对象
     *		field: 字段属性
     *		cacheMap: 额外参数（已包含缓存数据）
     */
    default void handle(HandleFullRecord fullRecord , Map<K,V> cacheMap){
        HandleRecord record = fullRecord.toRecord();
        //翻译后的值
        Object o = doHandle(record, cacheMap);
        if(null != o){
            Map<String, Object> item = fullRecord.item;
            Translator translator = fullRecord.translator;
            String rename;
            if(translator.replace()){
                rename = fullRecord.params.name;
            }else {
                if(StringUtils.isNotBlank(translator.rename())){
                    rename = translator.rename();
                }else if(StringUtils.isNotBlank(record.getRename())){
                    rename = record.getRename();
                }else{
                    rename = fullRecord.params.name + TranslatorConfig.DEFAULT_SUFFIX;
                }
            }
            item.put(rename,o);
            if( ! TranslatorAbleHelper.tran2Map) {
                //如果不能转化成Map.需要特殊操作
                Object curObj = fullRecord.record;
                //原值替换操作
                record.replace(o);
                //翻译结果放在Map里
                if(curObj instanceof TransEntity){
                    TransEntity tranEntity = (TransEntity) curObj;
                    tranEntity.transResult.put(rename,o);
                }
            }
            if(log.isDebugEnabled()){
                Object value = record.getVal();
                log.debug("翻译：{}[{}] -> {}[{}] ", fullRecord.params.name,value,rename,o);
            }
        }
    }

    Object doHandle(HandleRecord record , Map<K,V> cacheMap);

    /**
     * @Description:  预留缓存方法
     * @Author: xwduo
     * @Date: 2022-10-28 17:11
     * @param：
     *		recordList: 数据缓存处理
     * * @return Map<Object>
     * @param recordList
     */
    Map<K,V> cache(List<HandleFullRecord> recordList);

    int order();

    default List<String> fetchParams(String[] extraParams ,String prefix) {
       if(null != extraParams && extraParams.length>0){
           return Arrays.stream(extraParams).filter(str -> str.startsWith(prefix)).map(str -> str.substring(prefix.length())).collect(Collectors.toList());
       }
       return Collections.emptyList();
    }

}
